package com.cidp.system.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.Taffiche;
import com.cidp.system.model.Toperator;
import com.cidp.system.pojo.AffichePo;
import com.cidp.system.service.IAfficheService;
import com.cidp.system.service.IOperatorService;
import com.litt.core.common.Constants;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;

/**
 * <b>标题：</b>系统公告.
 * <pre><b>描述</b>
 *    系统公告管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-6-3
 * @version 1.0
 */
public class AfficheAction  extends DispatchAction{

	private IAfficheService afficheService;
	private IOperatorService operatorService;
	
	/**
	 * 查看通知公告
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		AffichePo affichePo = (AffichePo)dynaActionForm.get("affichePo");
		if(affichePo.getValidDays() != null && affichePo.getValidDays().intValue() == 0)
			affichePo.setValidDays(null);
		
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = afficheService.listByPage(new PagePoParam(affichePo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		for(int i=0;i<page.getRsList().size();i++){
			Taffiche affiche = (Taffiche) page.getRsList().get(i);
			Toperator operator = operatorService.load(affiche.getOpId());
			affiche.setOpName(operator.getOpName());
		}
		
		
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("toList");
	}

	/**
	 * 保存通知公告
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		AffichePo affichePo = (AffichePo)dynaActionForm.get("affichePo");
		
		Taffiche affiche = new Taffiche();
		affiche.setType(affichePo.getType());
		affiche.setContent(affichePo.getContent());
		affiche.setOpId(affichePo.getOpId());
		affiche.setTitle(affichePo.getTitle());
		affiche.setValidDays(affichePo.getValidDays());
		//操作员信息未保存，这里先写死
		affiche.setOpId(new Long(1));
		afficheService.saveAffiche(affiche);
		
		request.setAttribute("successFlag","affiche_saveAffiche");
		return mapping.findForward("toSave");
	}
	
	/**
	 * 新增通知公告
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 修改通知公告
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		AffichePo affichePo = (AffichePo)dynaActionForm.get("affichePo");
		String id = request.getParameter("id");
		Taffiche affiche = afficheService.loadById(new Long(id));
		try{
			BeanUtils.copyProperties(affichePo, affiche);
		}catch(Exception e){
			
		}
		dynaActionForm.set("affichePo", affichePo);
		return mapping.findForward("toEdit");
	}
	
	/**
	 * 更新通知公告
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		AffichePo affichePo = (AffichePo)dynaActionForm.get("affichePo");
		Taffiche affiche = afficheService.loadById(affichePo.getAfficheId());
		affiche.setContent(affichePo.getContent());
		affiche.setTitle(affichePo.getTitle());
		affiche.setValidDays(affichePo.getValidDays());
		afficheService.updateAffiche(affiche);
		
		request.setAttribute("successFlag","affiche_updateAffiche");
		return mapping.findForward("toUpdate");
	}
	public void setAfficheService(IAfficheService afficheService) {
		this.afficheService = afficheService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}
}
